<template>
  <div class="der-page">
    <div class="video-page">
      <div class="video-left">
        <video ref="videoRef" class="video-class" controls crossorigin="anonymous" :autoplay="true" disablePictureInPicture="true">
          <source type="video/ogg" src="" />
          <source type="video/mp4" src="" />
          <source type="video/webm" src="" />
        </video>
        <div class="video-title">
          <span class="footer-left">{{ title + '-' + openLink?.fileName }}</span>
          <a-button class="ml-8 download-btn" type="text" @click="downLoadOneVideo(openLink)"
            ><i class="iconfont icon-download-2-line mr-6"></i>下载
          </a-button>
        </div>
      </div>
      <div class="video-right">
        <page-right-list :text-content="spans" style="height: 598px">
          <template #list>
            <div class="select-list my-scrollbar">
              <div class="select-header">
                <div class="header-text">
                  <span class="header-text-w">视频</span>
                  <span class="open-video-num">{{ openNumber }}/{{ data.length }}</span>
                </div>
                <div class="header-down">
                  <a-button type="link" @click="downloadAll">全部下载</a-button>
                </div>
              </div>
              <div class="line-svg"></div>
              <div class="list-content-box my-scrollbar">
                <div
                  v-for="(item, index) in data"
                  :key="index"
                  class="list-content"
                  style="width: 100%"
                  :class="[item.status ? 'active' : '']"
                  @click="handleBtn(item.id, index)"
                >
                  <span class="list-content-left elliptical-1" :title="item.fileName">{{ item.fileName }}</span>
                  <span class="list-content-right list-time">{{ item.videoDuration ? item.videoDuration : '' }}</span>
                  <span class="list-download-show list-download iconfont icon-download-2-line" @click="downLoadOneVideo(item)"></span>
                </div>
              </div>
            </div>
          </template>
        </page-right-list>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
  import { message } from 'ant-design-vue'
  import { nextTick, ref, watch } from 'vue'

  import manualMessage from '@/components/ManualMessage/index'
  import { fetchDownload } from '@/services/api'

  import { FilesStatusType } from '../../types/index'
  import PageRightList from './PageRightList.vue'
  export interface LabelList {
    fileName: string
  }
  export interface DataSetCardProps {
    data?: FilesStatusType[]
    spans?: any
    title: string
  }
  let props = withDefaults(defineProps<DataSetCardProps>(), {
    data: () => [],
    title: '~'
  })

  let videoRef = ref()

  // 播放数量
  const openNumber = ref<number>(1)
  let watchData = watch(
    () => props.data,
    () => {
      if (props.data.length > 0) {
        watchData()
        //eslint-disable-next-line
        props.data[0]['status'] = true
        openLink.value = props.data[0]
        uploadVideo()
      }
    }
  )
  const openLink = ref<any>()

  const handleBtn = (id: number, num: number) => {
    openNumber.value = num + 1
    props.data.map((item: any) => {
      if (id !== item.id) {
        item['status'] = false
      } else {
        item['status'] = true
        openLink.value = item
        uploadVideo()
      }
    })
  }

  // 全部下载
  const downloadAll = async () => {
    manualMessage.loading('数据准备中,请耐心等待...', 0)
    const downloadPromiseList: Promise<void>[] = []
    props.data.forEach(async obj => {
      downloadPromiseList.push(fetchDownload(obj.url, `${obj.fileName}`))
    })
    Promise.all(downloadPromiseList)
      .then(() => {
        manualMessage.destroy()
        message.success({ content: '文件下载成功', icon: '', key: 'downloading', duration: 2 })
      })
      .catch(() => {
        manualMessage.destroy()
        message.error('下载失败')
      })
  }

  // 单个下载
  const downLoadOneVideo = async (obj: FilesStatusType) => {
    try {
      manualMessage.loading('数据准备中,请耐心等待...', 0)
      await fetchDownload(obj.url, `${obj.fileName}`)
      manualMessage.destroy()
      await message.success({ content: '文件下载成功', icon: '', key: 'downloading', duration: 2 })
    } catch (error: any) {
      console.log(error)
      manualMessage.destroy()
      message.error('下载失败')
    }
  }

  // 加载video
  const uploadVideo = () => {
    nextTick(() => {
      videoRef.value.load()
      videoRef.value.src = openLink.value.url
    })
  }

  const pauseVideo = () => {
    videoRef.value.pause()
  }
  defineExpose({ pauseVideo })
</script>
<style lang="less" scoped>
  .der-page {
    width: 100%;
  }

  .video-page {
    display: flex;
    width: 100%;
    gap: 24px;

    .video-left {
      width: 1026px;
      flex-shrink: 0;
      border-radius: 10px;

      .video-class {
        width: 100%;
        height: 598px;
        border-radius: 10px;
        box-shadow: 0 2px 8px 0 rgba(37, 37, 45, 0.14), 0 1px 3px 0 rgba(37, 37, 45, 0.14);
      }

      .video-title {
        display: flex;
        justify-content: space-between;
        align-items: flex-end;
        margin-top: 22px;
        width: 100%;

        .footer-left {
          color: @text01;
          .font16b();
        }

        .download-btn {
          width: 84px;
          background: @background5;
          border-radius: 6px;
        }
      }
    }

    .video-right {
      overflow: hidden;
      width: 328px;

      .select-list {
        display: flex;
        align-items: flex-start;
        padding: 20px 12px;
        margin-top: 16px;
        width: 328px;
        height: 416px;
        background: #ebf0f7;
        border: 1px solid @border1;
        border-radius: 10px;
        flex-direction: column;
        gap: 12px;
        flex: 1 0 0;

        .select-header {
          display: flex;
          justify-content: space-between;
          align-items: center;
          padding: 0 8px;
          width: 100%;

          .header-text {
            display: flex;
            gap: 12.5px;
            align-items: center;

            .header-text-w {
              color: @text02;
              .font14b();
            }

            .open-video-num {
              color: @text03;
              .font14();
            }
          }
        }

        .line-svg {
          margin-bottom: 12px;
          width: 100%;
          height: 0;
          border-top: 1px solid @border1;
        }

        .list-content {
          display: flex;
          justify-content: space-between;
          align-items: center;
          padding: 0 8px;
          width: 100%;
          height: 36px;
          min-height: 36px;
          border-radius: 6px;
          cursor: pointer;

          &:hover {
            background: rgba(0, 0, 0, 0.05);

            .list-download-show {
              display: block;
              color: #009cf8;
            }

            .list-time {
              display: none;
            }
          }

          &.active {
            background: #fff;
            font-weight: 700;
          }

          & + .list-content {
            margin-top: 2px;
          }

          .list-content-left {
            color: @text01;
            .font14();
          }

          .list-content-right {
            color: @text03;
            .font14();
          }

          .list-download {
            display: none;
          }
        }

        .list-content-box {
          display: flex;
          overflow-y: auto;
          width: 100%;
          flex-direction: column;
          min-height: 280px;
        }
      }
    }
  }
</style>
